package com.codeying.servlet.yuyue;

import com.codeying.servlet.AbsServlet;
import com.codeying.entity.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;
import com.codeying.utils.*;
import com.codeying.utils.component.*;

/**
 * 保存和新增预约试驾
 */
@WebServlet("/yuyue/save")
public class SaveYuyueServlet extends AbsServlet {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setContentType("application/json; charset=utf-8");
        resp.setCharacterEncoding("UTF-8");
        int res=0;
        Yuyue entityTemp=new Yuyue ();
        //将参数全部转化成Map格式
        Map<String, Object> reqMap = CommonUtils.requestToMap(req);
        String id = (String) reqMap.get("id");
        entityTemp.setId(Integer.parseInt(id));
                                        String userid = ""; //用户
                                            String cid = ""; //汽车
                                            Date shijian = null; //预约试驾时间
                                            String shuom = ""; //预约说明
                                            String jieguo = ""; //预约结果
                                        userid = (String)reqMap.get("userid");
            entityTemp.setUserid (Integer.parseInt(userid));//String类型参数
                cid = (String)reqMap.get("cid");
            entityTemp.setCid (Integer.parseInt(cid));//String类型参数
                shijian = DateUtil.strToDate((String)reqMap.get("shijian"));
            entityTemp.setShijian (shijian);
                    shuom = (String)reqMap.get("shuom");
            entityTemp.setShuom (shuom);//String类型参数
                jieguo = (String)reqMap.get("jieguo");
            entityTemp.setJieguo (jieguo);//String类型参数

        //新增或更新
        if(entityTemp.getId() == 0 || "".equals(entityTemp.getId())){//新增
        id = String.valueOf(CommonUtils.newId());
        entityTemp.setId (Integer.parseInt(id));
        jieguo = "已预约";
        entityTemp.setJieguo (jieguo);
        //如果有唯一字段，则在此校验 （字段不为空在前端校验）
                                                                                        //=========添加之前
            
            //=========添加之前
            res = yuyueService.insert(entityTemp);
        }else {
            //=========修改之前
            
            //=========修改之前
            res = yuyueService.updateById(entityTemp);
        }

        if(res !=0){
            resp.getWriter().print(ApiResult.success());//标记保存成功
        }else {
            resp.getWriter().print(ApiResult.fail("保存失败"));//保存失败
        }
    }
}

